package com.eazyrpc.bigdata.sql;


import com.eazyrpc.thrift.base.SQLService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;


public class RPCClient {
    public String select(String sql){
        TTransport tTransport = null;
        try {
            tTransport = new TSocket("localhost",9090,30000);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(tTransport);
//            TProtocol protocol = new TCompactProtocol(tTransport);
//            TProtocol protocol = new TJSONProtocol(tTransport);
            SQLService.Client client = new SQLService.Client(protocol);
            tTransport.open();
            String result = client.select(sql);
            return result;
        } catch (TTransportException ex) {
            ex.printStackTrace();
            return "";
        } catch (TException ex) {
            ex.printStackTrace();
            return "";
        }finally {
            if(tTransport != null){
                tTransport.close();
            }
        }
    }


}
